define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'carbon/calculation/info/index' + location.search,
                    add_url: 'carbon/calculation/info/add',
                    edit_url: 'carbon/calculation/info/edit',
                    del_url: 'carbon/calculation/info/del',
                    multi_url: 'carbon/calculation/info/multi',
                    import_url: 'carbon/calculation/info/import',
                    table: 'carbon_calculation_info',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        { field: 'type_superior.name', title: __('碳中和类型') },
                        { field: 'type.name', title: __('Type') },
                        {
                            field: 'energy.name',
                            title: __('Type_energy'),
                            operate: 'LIKE',
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'name',
                                    text: function (row) {
                                        var energy = row.energy;
                                        var energyName = '<button class="btn btn-xs btn-success btn-action" data-id="' + row.id + '" data-url="/carbon/energy/info?calculation_id=' + row.id + '"><i class="fa fa-plus"></i> ' + __('添加') + '</button>';
                                        for (var i = 0; i < energy.length; i++) {
                                            if (i == 0) {
                                                energyName = i + 1 + '. ' + energy[i].name;
                                            } else {
                                                var a = i + 1;
                                                energyName = energyName + '<br>' + a + '. ' + energy[i].name;
                                            }
                                        }
                                        return energyName;
                                    },
                                    title: function (row) {
                                        return row.name + '-' + __('Type_energy')
                                    },
                                    classname: 'btn btn-xs btn-dialog',
                                    extend: 'data-area=\'["100%", "100%"]\'',
                                    url: 'carbon/energy/info?calculation_id={id}',
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        },

                        { field: 'memo', title: __('Memo'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        { field: 'status', title: __('Status'), searchList: { "normal": __('Normal'), "hidden": __('Hidden') }, formatter: Table.api.formatter.status },
                        // { field: 'create_time', title: __('Create_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        // { field: 'update_time', title: __('Update_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
